package com.sc.nft.dao;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.ActivityBasics;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.UserPrizeDrawRecords;
import com.sc.nft.entity.dto.MyGuessDTO;
import com.sc.nft.entity.dto.NumberQueryListDTO;
import com.sc.nft.entity.vo.NumberQueryListVO;
import com.sc.nft.entity.vo.WinnersListVO;
import com.sc.nft.enums.PrizeDrawActivityRewardsEnum;
import com.sc.nft.enums.PrizeDrawActivityTypeEnum;
import com.sc.nft.mapper.UserPrizeDrawRecordsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户抽奖号码表(sc_user_prize_draw_records)数据DAO
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2023-09-14 11:58:29
 */
@Slf4j
@Repository
public class UserPrizeDrawRecordsDao extends ServiceImpl<UserPrizeDrawRecordsMapper, UserPrizeDrawRecords> {


    /**
     * 根据活动id和用户id验证他的活动编号.把重复的获取出来
     *
     * @return
     */
    public List<UserPrizeDrawRecords> withActivityIdVerifyUsersSerialNumber(Long activityId, Long userId, List<String> betting) {
        LambdaQueryWrapper<UserPrizeDrawRecords> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(UserPrizeDrawRecords::getActivityId, activityId);
        lambdaQueryWrapper.eq(UserPrizeDrawRecords::getUserId, userId);
        lambdaQueryWrapper.in(UserPrizeDrawRecords::getLotteryDrawResult, betting);
        lambdaQueryWrapper.groupBy(UserPrizeDrawRecords::getLotteryDrawResult);
        return list(lambdaQueryWrapper);
    }

    public Integer getUserActivitySum(Long activityId, Long userId) {
        LambdaQueryWrapper<UserPrizeDrawRecords> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(UserPrizeDrawRecords::getActivityId, activityId);
        lambdaQueryWrapper.eq(UserPrizeDrawRecords::getUserId, userId);
        return count(lambdaQueryWrapper);
    }
    public Page<UserPrizeDrawRecords> myGuess(MyGuessDTO myGuessDTO, UserInfo user, ActivityBasics activityBasics) {
        LambdaQueryWrapper<UserPrizeDrawRecords> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(UserPrizeDrawRecords::getActivityId, myGuessDTO.getActivityId());
        lambdaQueryWrapper.eq(UserPrizeDrawRecords::getUserId, user.getId());
        if (activityBasics.getLotteryStatus().equals(1)) {
            if (activityBasics.getType().equals(PrizeDrawActivityTypeEnum.GUESSING_ACTIVITY)) {
                lambdaQueryWrapper.orderByDesc(UserPrizeDrawRecords::getIsWinPrizeLottery);
                lambdaQueryWrapper.orderByAsc(UserPrizeDrawRecords::getGuessType);
            }
            if (activityBasics.getType().equals(PrizeDrawActivityTypeEnum.PARTITION_ACTIVITY)) {
                lambdaQueryWrapper.orderByDesc(UserPrizeDrawRecords::getIsWinPrizeLottery);
                lambdaQueryWrapper.orderByAsc(UserPrizeDrawRecords::getType);
            }
        }else {
            lambdaQueryWrapper.orderByAsc(UserPrizeDrawRecords::getId);
        }
        return page(myGuessDTO.toMp(), lambdaQueryWrapper);
    }
    public List<WinnersListVO> listWinners(Long id) {
        return baseMapper.listWinners(id);
    }
    public List<NumberQueryListVO> numberQueryList(NumberQueryListDTO numberQueryListDTO) {
        return baseMapper.numberQueryList(numberQueryListDTO);
    }


    /**
     * 干掉参与竞猜的用户重复票据
     *
     * @param activityId
     */
    public void losingLottery(Long activityId) {
        List<Integer> activityIdRepeat = baseMapper.getActivityIdRepeat(activityId);
        if (CollUtil.isEmpty(activityIdRepeat)) {
            return;
        }
        baseMapper.losingLottery(activityId,activityIdRepeat);
    }

    public void losingLotteryAll(Long activityId) {
        baseMapper.losingLotteryAll(activityId);
    }


    /**
     * 中奖数据
     *
     * @param activityId 活动数据
     * @param number 取多少个
     * @param overturn 是否翻转,这个翻转是考虑到获取的时候前面的不够 就要反过来在取
     * @return
     */
    public List<UserPrizeDrawRecords> winAPrizeLottery(Long activityId, Integer result, Integer number, Boolean overturn) {
        return baseMapper.winAPrizeLottery(activityId,result,number,overturn);
    }
    public Long numberPeopleDrawn(Long id) {
        return baseMapper.numberPeopleDrawn(id);
    }

    public Long numberDraws(Long id) {
        return baseMapper.numberDraws(id);
    }

    public List<UserPrizeDrawRecords> getWinningNumberByActivityIdAndNumber(Long activityId, Integer v1, Integer v2, Integer v3, Integer v4, Integer v5,ArrayList<Long> notInIds) {
        return baseMapper.getWinningNumberByActivityIdAndNumber(activityId,v1,v2,v3,v4,v5,notInIds);
    }

    public Boolean updateWinPrizeLotteryAndTypeNameByIds(Long activityId, ArrayList<Long> ids, PrizeDrawActivityRewardsEnum type, String name) {
        LambdaUpdateWrapper<UserPrizeDrawRecords> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(UserPrizeDrawRecords::getId,ids);
        wrapper.eq(UserPrizeDrawRecords::getActivityId,activityId);
        wrapper.set(UserPrizeDrawRecords::getIsWinPrizeLottery,2);
        wrapper.set(UserPrizeDrawRecords::getType,type);
        wrapper.set(UserPrizeDrawRecords::getTypeName,name);
        return update(wrapper);
    }

    public Boolean updateLosingLotteryAndTypeNameByIds(Long activityId, ArrayList<Long> notInIds) {
        LambdaUpdateWrapper<UserPrizeDrawRecords> wrapper = new LambdaUpdateWrapper<>();
        wrapper.notIn(notInIds.size()>0,UserPrizeDrawRecords::getId,notInIds);
        wrapper.eq(UserPrizeDrawRecords::getActivityId,activityId);
        wrapper.set(UserPrizeDrawRecords::getIsWinPrizeLottery,1);
        wrapper.set(UserPrizeDrawRecords::getTypeName,"未中奖");
        return update(wrapper);
    }
}